<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        table,
        td {
            border-collapse: collapse;
        }
        
        td,
        th {
            width: 200px;
            height: 30px;
            border: 1px solid rgb(209, 209, 209);
            text-align: center;
            padding: 10px 0;
        }
        
        td button {
            padding: 0 20px;
            height: 24px;
        }
        
        .box {
            margin: 100px auto;
            width: 1000px;
        }
        
        .box h1 {
            text-align: center;
            margin-bottom: 20px;
        }
        
        .loading {
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, .5);
            color: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    </style>
</head>

<body>
    <div class="box">
        <h1>白金会员管理系统</h1>
        <div class="list">
            <table>
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>手机号</th>
                        <th>真实姓名</th>
                        <th>密码</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr>
                        <td>1</td>
                        <td>1</td>
                        <td>1</td>
                        <td>1</td>
                    </tr> -->
                </tbody>
            </table>
        </div>
    </div>

    <div class="loading">
        拼命加载
    </div>

    <script>
        function selectAll() {
            var tbody = document.querySelector("tbody");
            var logding = document.querySelector(".loading");

            var xhr = new XMLHttpRequest();
            xhr.open("get", './04_selectAll.php', true);
            xhr.send();

            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var data = JSON.parse(xhr.response);
                    if (data.code == 0) {
                        let html = "";
                        data.list.forEach(key => {
                            html += `
                            <tr>
                                <td>${key.Id}</td>
                                <td>${key.username}</td>
                                <td>${key.tel}</td>
                                <td>${key.pw}</td>
                                <td><button data-id=${key.Id}>删除</button></td>
                            </tr>
                            `
                        })
                        tbody.innerHTML = html;
                        logding.style.display = "none"
                    } else {
                        console.log(2)
                    }
                }
            }
        }

        function deleteRecord() {
            var tbody = document.querySelector("tbody");

            tbody.onclick = function(e) {
                if (e.target.tagName == "BUTTON") {
                    var id = parseInt(e.target.dataset.id);
                    var xhr = new XMLHttpRequest();
                    xhr.open('get', `./04_delete.php?Id=${id}`, true);
                    xhr.send();

                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            console.log(1)
                                // var data = JSON.parse(xhr.responseText);
                            selectAll();
                        }
                    }
                }
            }

        }
        selectAll();
        deleteRecord();
    </script>
</body>

</html>